package com.zsy.once.importuser;

import com.zsy.mapper.UserMapper;
import com.zsy.model.domain.User;
import com.zsy.service.UserService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 导入用户任务
 */
@Component
public class InsertUsers {

    @Resource
    private UserMapper userMapper;
    @Resource
    private UserService userService;

    /**
     * 批量插入用户
     */
 //   @Scheduled(initialDelay = 5000, fixedRate = Long.MAX_VALUE)//
    public void doInsertUsers() {
        // 程序执行时间过长，使用 StopWatch 计时
        StopWatch stopWatch = new StopWatch();
        System.out.println("开始批量插入用户数据");
        stopWatch.start();
        final int INSERT_NUM = 100000;
        List<User> userList = new ArrayList<>();
        for (int i = 0; i < INSERT_NUM; i++) {
            User user = new User();
            user.setUsername("测试用户"+ i);
            user.setUserAccount("fakeAccount" + i);
            user.setAvatarUrl("https://tse1-mm.cn.bing.net/th/id/OIP-C.Hm8mMegmkYy7K58y4VRYngHaHa?rs=1&pid=ImgDetMain");
            user.setGender(0);
            user.setUserPassword("12345678");
            user.setPhone("123");
            user.setEmail("123@qq.com");
            user.setTags("[]");
            user.setUserStatus(0);
            user.setUserRole(0);
            user.setPlanetCode("11111111");
            userList.add(user);
            //userMapper.insert(user);
            userService.saveBatch(userList,10000);

        }
        stopWatch.stop();
        System.out.println("消耗时间"+(stopWatch.getTotalTimeMillis()/1000)+"秒");
    }
}
